package com.administrator.scoreManageSys.controller.student;

import com.administrator.scoreManageSys.service.StudentExamMangeService;
import com.alibaba.fastjson.JSONObject;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER;
import static com.administrator.scoreManageSys.system.SystemConstant.CURRENT_SYSTEM_USER_ROLE;

/**
 * @author zsh
 * @date 2022/04/12/ 11:36
 */
@RestController
@RequestMapping(value = "/student/exam")
public class StudentExamMangeController {
    @Resource
    private StudentExamMangeService studentExamMangeService;

    @PostMapping(value = "/obtainMyOwnExamList")
    @RequiresRoles("studentInfo")
    @RequiresPermissions(value = "member:edit", logical = Logical.OR)
    public JSONObject obtainMyOwnExamList(HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            Long role = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER_ROLE).toString());
            return studentExamMangeService.obtainMyOwnExamList(userId,role);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("添加成绩数据获取 异常");
        }
    }

    @PostMapping(value = "/gradeStudents")
    @RequiresRoles("studentInfo")
    @RequiresPermissions(value = "member:edit", logical = Logical.OR)
    public JSONObject gradeStudents(@RequestBody JSONObject data,HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            Long studentId = data.getLong("studentId");
            Long season = data.getLong("season");
            Long subject = data.getLong("subject");
            Long examination = data.getLong("examination");
            Double score = data.getDouble("score");
            return studentExamMangeService.gradeStudents(userId,studentId,season,subject,examination,score);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("给学生打分 异常");
        }
    }

    @PostMapping(value = "/getStudentExamList")
    @RequiresRoles("studentInfo")
    @RequiresPermissions(value = "member:edit", logical = Logical.OR)
    public JSONObject getStudentExamList(@RequestBody JSONObject data,HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            Long roleId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER_ROLE).toString());
            Long studentName = data.getLong("studentName");
            Long season = data.getLong("season");
            Long classId = data.getLong("classId");
            return studentExamMangeService.getStudentExamList(userId,studentName,season,classId,roleId);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("获取学生成绩列表 异常");
        }
    }

    @PostMapping(value = "/modifyGrades")
    @RequiresRoles("studentInfo")
    @RequiresPermissions(value = "member:edit", logical = Logical.OR)
    public JSONObject modifyGrades(@RequestBody JSONObject data,HttpServletRequest request) {
        try {
            //操作用户id
            Long userId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER).toString());
            Long roleId = Long.valueOf(request.getAttribute(CURRENT_SYSTEM_USER_ROLE).toString());
            Long studentId = data.getLong("studentId");
            Long examination = data.getLong("examination");
            Double score = data.getDouble("score");
            return studentExamMangeService.modifyGrades(userId,roleId,studentId,examination,score);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("修改成绩 异常");
        }
    }
}
